/**
 * 
 */
package fhq.hcmute.modernshopping.fragment;

import java.lang.reflect.Type;
import java.util.ArrayList;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import fhq.hcmute.modernshopping.ProjectUtil;
import fhq.hcmute.modernshopping.R;
import fhq.hcmute.modernshopping.adapter.Comment;
import fhq.hcmute.modernshopping.adapter.Product;
import fhq.hcmute.modernshopping.adapter.ProductCommentAdapter;
import fhq.hcmute.modernshopping.webservice.Webservice;

/**
 * @author: Hien Nguyen Le
 * @emai: hien.nguyenle.it@gmail.com
 * 
 */
public class ProductComments extends Fragment {
	private static ProductComments instance = null;
	private ArrayList<Comment> listComments;
	private ListView lv_ListComment;

	public static ProductComments getProductComments(Activity activity,
			Product product) {
		if (instance == null) {
			instance = new ProductComments();
			String url = ProjectUtil.WEB_SERVICE_LINK
					+ "GetProductComment?ProductCode=%d&Take=10";
			url = String.format(url, product.getCode());
			new Webservice(activity, instance.productInStoresHandler)
					.execute(url);
		}
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View root = inflater.inflate(R.layout.fragment_product_comments,
				container, false);
		lv_ListComment = (ListView) root.findViewById(R.id.lv_list_comment);
		lv_ListComment.setOnItemClickListener(onStoreItemClickListener);
		if (instance != null && listComments != null) {
			ProductCommentAdapter productCommentAdapter = new ProductCommentAdapter(
					ProductComments.instance.getActivity(), listComments);
			lv_ListComment.setAdapter(productCommentAdapter);
		}

		return root;
	}

	private OnItemClickListener onStoreItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3) {
			Comment comment = listComments.get(position);
			MyDialog dialog = MyDialog.newInstance(comment.getUserComment());
			dialog.show(getFragmentManager(), "Dialog");
		}
	};
	private Handler productInStoresHandler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(Message msg) {
			if (msg.what == Webservice.GET_DATA_SUCCESSFUL) {
				String result = (String) msg.obj;
				Gson gson = new Gson();
				Type collectionType = new TypeToken<ArrayList<Comment>>() {
				}.getType();
				listComments = (ArrayList<Comment>) gson.fromJson(result,
						collectionType);
				if (listComments != null) {
					ProductCommentAdapter productCommentAdapter = new ProductCommentAdapter(
							ProductComments.instance.getActivity(),
							listComments);
					lv_ListComment.setAdapter(productCommentAdapter);
				} 
			} else if (msg.what == Webservice.GET_DATA_FAILED) {

			}
			super.handleMessage(msg);
		}
	};

	public static void destroyInstance() {
		ProductComments.instance = null;
	}
}
